package com.yungu.swift.socket.communication.server.handler.strategy;

import com.google.gson.Gson;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.SpringContextHolder;
import com.yungu.swift.lbs.model.DriverLocation;
import com.yungu.swift.lbs.model.DriverPoint;
import com.yungu.swift.lbs.service.LocationService;
import com.yungu.swift.socket.communication.message.GetDriverLocationMessage;
import com.yungu.swift.socket.communication.message.GetOrderLocationMessage;
import com.yungu.swift.socket.communication.message.GetOrderLocationResponseMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

/**
 * 获取司机订单位置响应
 *
 * @author : cuixiuyin
 * @date : 2019/8/30
 */
public class DriverOrderLocationHandler implements MessageHandler {

    @Override
    public void handler(ChannelHandlerContext channelHandlerContext, String text) {
        LocationService locationService = SpringContextHolder.getBean(LocationService.class);
        Gson gson = new Gson();
        //将报文转换成可保存的实体
        GetOrderLocationMessage getOrderLocationMessage = gson.fromJson(text, GetOrderLocationMessage.class);
        //处理业务逻辑
        ResponseData<DriverLocation> responseData = locationService.findNewestOrderLocation(
                getOrderLocationMessage.getOrderUuid(), getOrderLocationMessage.getAppid());
        DriverLocation driverLocation = responseData.getData();
        GetOrderLocationResponseMessage getOrderLocationResponseMessage;
        if (driverLocation != null) {
            getOrderLocationResponseMessage = new GetOrderLocationResponseMessage(getOrderLocationMessage.getClientUuid(), driverLocation.getUploadTime().getTime(), driverLocation.getCoordinate().getLng(), driverLocation.getCoordinate().getLat(), driverLocation.getMileage(), driverLocation.getFee(), true, "获取订单中的司机最新位置成功");
        } else {
            GetDriverLocationMessage getDriverLocationMessage = new GetDriverLocationMessage(getOrderLocationMessage.getClientUuid(), getOrderLocationMessage.getDriverUuid(), getOrderLocationMessage.getAppid());
            ResponseData<DriverPoint> responseData1 = locationService.findDriverPoint(
                    getDriverLocationMessage.getDriverUuid(), getDriverLocationMessage.getAppid());
            DriverPoint driverPoint = responseData1.getData();
            if (driverPoint != null) {
                getOrderLocationResponseMessage = new GetOrderLocationResponseMessage(getOrderLocationMessage.getClientUuid(), driverPoint.getUploadTime().getTime(), driverPoint.getCoordinate().getLng(), driverPoint.getCoordinate().getLat(), driverPoint.getMileage(), 0D, true, "订单不存在，获取司机最新位置成功");
            } else {
                getOrderLocationResponseMessage = new GetOrderLocationResponseMessage(getOrderLocationMessage.getClientUuid(), 0L, 0D, 0D, 0D, 0D, false, "获取司机最新位置失败");
            }
        }
        //给响应客户端响应处理结果
        channelHandlerContext.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(getOrderLocationResponseMessage)));
    }
}
